from ConfigParser import SafeConfigParser

class Configuration:
	def __init__ (self, fileName):
		cp = SafeConfigParser()
		cp.read(fileName)
		self.__parser = cp
		self.fileName = fileName

	def isSection(self, name):
		p = self.__parser
		return name in p.sections()
		
	def __getattr__ (self, name):
		if name in self.__parser.sections():
			return Section(name, self.__parser)
		else:
			return None
			
	def __str__ (self):
		p = self.__parser
		result = []
		result.append('<Configuration from %s>' % self.fileName)
		for s in p.sections():
			result.append('[%s]' % s)
			for o in p.options(s):
				result.append('%s=%s' % (o, p.get(s, o)))
		return '\n'.join(result)

class Section:
	def __init__ (self, name, parser):
		self.name = name
		self.__parser = parser
	def __getattr__ (self, name):
		return self.__parser.get(self.name, name)

if __name__ == '__main__':
	config = Configuration('config.ini')
	print config.dhcp.router
	print config.dhcp.dns
	print config.dhcp.domainName
	print config.dhcp.ntp
	print config.dhcp.defaultBoot
	print config.dhcp.listenPort
	print config.dhcp.emitPort
